from django.db import models

# Create your models here.
from django.db import models


# Create your models here.

class DateAbstract(models.Model):
    """
    时间公共继承模型
    """
    create_date = models.DateTimeField(auto_now_add=True)
    update_date = models.DateTimeField(auto_now_add=True)

    class Meta:
        abstract = True


class Project(DateAbstract):
    name = models.CharField(default='', max_length=100, verbose_name='项目名称')
    desc = models.TextField(verbose_name="项目描述", blank=True)
    status = models.CharField(max_length=20, blank=True, null=True, verbose_name="状态")
    excludes = models.TextField(blank=True, null=True, verbose_name='排除')
    is_include = models.BooleanField(default=False, verbose_name="包含")
    target_root = models.CharField(default='', max_length=200, verbose_name='部署路径')
    target_releases = models.CharField(default='', max_length=200, verbose_name='目标仓库')
    task_envs = models.TextField(blank=True, null=True, verbose_name='全局变量')
    prev_deploy = models.TextField(blank=True, null=True, verbose_name='deploy前置')
    post_deploy = models.TextField(blank=True, null=True, verbose_name='deploy后置')
    prev_release = models.TextField(blank=True, null=True, verbose_name='release前置')
    post_release = models.TextField(blank=True, null=True, verbose_name='release后置')
    version_num = models.IntegerField(blank=True, null=True, verbose_name='版本数量')
    repo_type = models.CharField(max_length=30, verbose_name='仓库类型')
    repo_url = models.CharField(default='', max_length=200, verbose_name='GTI仓库地址')
    repo_user = models.CharField(max_length=50, verbose_name='仓库用户名', blank=True, null=True)
    repo_password = models.CharField(max_length=50, verbose_name='仓库密码', blank=True, null=True)
    server_tags = models.IntegerField(blank=True, null=True, verbose_name='关联主机标签')
    deploy_mode = models.CharField(max_length=100, verbose_name='上线方式')
    last_task_status = models.CharField(max_length=20, default='', null=True, blank=True, verbose_name="状态")

    class Meta:
        verbose_name = '项目管理表'
        verbose_name_plural = verbose_name


class DeployOrder(DateAbstract):
    project = models.ForeignKey(Project, on_delete=models.SET_NULL, blank=True, null=True, verbose_name='绑定项目')
    name = models.CharField(max_length=200, verbose_name='发布单名称')
    desc = models.TextField(verbose_name='上线说明', blank=True)
    status = models.CharField(max_length=50, blank=True, default='Failed', verbose_name='状态')
    init_status = models.CharField(default='未初始化', max_length=80, verbose_name='初始化状态')

    class Meta:
        verbose_name = '上线单表'
        verbose_name_plural = verbose_name


class BuildRecord(DateAbstract):
    deploy_order_id = models.IntegerField(verbose_name='绑定的上线单ID')
    start_time = models.IntegerField(verbose_name='开始时间')
    end_time = models.IntegerField(verbose_name='停止时间')
    status = models.CharField(max_length=100, verbose_name='构建状态')
    output = models.TextField(verbose_name='输出')
    errmsg = models.TextField(verbose_name='错误信息')

    class Meta:
        verbose_name = '构建日志表'
        verbose_name_plural = verbose_name


class DeployRecord(DateAbstract):
    deploy_order_id = models.IntegerField(verbose_name='绑定的上线单ID')
    start_time = models.IntegerField(verbose_name='开始时间')
    end_time = models.IntegerField(verbose_name='停止时间', blank=True, null=True)
    status = models.CharField(max_length=100, verbose_name='状态')
    output = models.TextField(verbose_name='输出', blank=True, null=True)
    errmsg = models.TextField(verbose_name='错误信息', blank=True, null=True)

    class Meta:
        verbose_name = '部署日志表'
        verbose_name_plural = verbose_name
